﻿using MT.Common.Cache;
using MT.Common.Helper;
using MT.Models.Entity;
using MT.Mvc.Core.Filters;
using MT.WebApi.Core.Filters;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization;
using StackExchange.Profiling;
using System;

namespace MT.WebApi.Core.ConfigureServices
{
    public static class MvcConfiguration
    {

        public static void Build(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddDbContext<AppDbContext>(options => options.UseSqlServer(configuration.GetConnectionString("DefautConnection")), ServiceLifetime.Scoped);

            services.AddMvc(
              options =>
              {
                  options.Filters.Add(typeof(GlobalExceptionFilter));
                  options.Filters.Add(typeof(GlobalAuthorizationFilter));
                  options.Filters.Add(typeof(GlobalActionFilter));
                  options.Filters.Add(typeof(GlobaResultFilter));

                  options.RespectBrowserAcceptHeader = true;
              }
              ).AddJsonOptions(options =>
              {
                  options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                  options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
                  options.SerializerSettings.DateFormatString = "yyyy/MM/dd HH:mm:ss";
              });


            services.AddSingleton(typeof(ICacheService), new RedisCacheService(new RedisCacheOptions
            {
                Configuration = ConfigHelper.GetSection("CacheConfiguration", "Redis_ConnectionString"),
                InstanceName = ConfigHelper.GetSection("CacheConfiguration", "Redis_InstanceName")

            }, 0));

            services.AddSingleton<IMemoryCache>(factory =>
            {
                var cache = new MemoryCache(new MemoryCacheOptions());
                return cache;
            });
          
            services.AddCors(c =>
            {
                c.AddPolicy("AllowAnyOrigin", policy =>
                {
                    policy.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                });
            });

            services.SwaggerServer();
            services.TokenService();
            services.DependencyResolverService();

            services.AddMiniProfiler(options =>
            {
                options.PopupRenderPosition = RenderPosition.BottomLeft;
                options.PopupShowTimeWithChildren = false;
                options.RouteBasePath = "/profiler";
            })
            .AddEntityFramework();
        }


        private static void UseRouting(this IServiceCollection services)
        {
            // 采用小写的 URL 路由模式
            services.AddRouting(options =>
            {
                options.LowercaseUrls = true;
            });
        }
    }
}
